package com.ylb.rent.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylb.rent.config.PageConfig;
import com.ylb.rent.entity.vo.ApartmentDetailVo;
import com.ylb.rent.util.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;

import com.ruoyi.common.enums.BusinessType;
import  com.ylb.rent.entity.Apartment;
import com.ylb.rent.service.IApartmentService;


/**
 * 公寓Controller
 * 
 * @author ylb
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/apartment/apartment")
public class ApartmentController
{
    @Autowired
    private IApartmentService apartmentService;

    @GetMapping("/list/city")
    public String listDetailByCityId(long cId){
        PageHelper.startPage(1,6);
        List<ApartmentDetailVo> apartmentDetailVos = apartmentService.selectVoListByCityId(cId);
        return R.successJSONData(new PageInfo<>(apartmentDetailVos));
    }
    @GetMapping("/list/detail")
    public String listDetail(long aId){
        ApartmentDetailVo apartmentDetailVo = apartmentService.selectApartmentVoById(aId);
        return R.successJSONData(apartmentDetailVo);
    }
    /**
     * 查询公寓列表
     */
//    @PreAuthorize("@ss.hasPermi('apartment:apartment:list')")
    @GetMapping("/list")
    public String list(Apartment apartment, PageConfig pageConfig)
    {
        PageHelper.startPage(pageConfig);
        List<Apartment> list = apartmentService.selectApartmentList(apartment);
        return R.successJSONData(new PageInfo<>(list));
    }




    /**
     * 获取公寓详细信息
     */
//    @PreAuthorize("@ss.hasPermi('apartment:apartment:query')")
    @GetMapping(value = "/{id}")
    public String getInfo(@PathVariable("id") Long id)
    {
        return R.successJSONData(apartmentService.selectApartmentById(id));
    }

    /**
     * 新增公寓
     */
    @PreAuthorize("@ss.hasPermi('apartment:apartment:add')")
    @Log(title = "公寓", businessType = BusinessType.INSERT)
    @PostMapping
    public String add(@RequestBody ApartmentDetailVo apartmentVo)
    {

        return R.successJSONData(apartmentService.insertApartment(apartmentVo));
    }

    /**
     * 修改公寓
     */
    @PreAuthorize("@ss.hasPermi('apartment:apartment:edit')")
    @Log(title = "公寓", businessType = BusinessType.UPDATE)
    @PutMapping
    public String edit(@RequestBody ApartmentDetailVo apartmentVo)
    {
        return R.successJSONData(apartmentService.updateApartment(apartmentVo));
    }

    /**
     * 删除公寓
     */
    @PreAuthorize("@ss.hasPermi('apartment:apartment:remove')")
    @Log(title = "公寓", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public String remove(@PathVariable Long[] ids)
    {
        return R.successJSONData(apartmentService.deleteApartmentByIds(ids));
    }
}
